package www.houdoudou.com.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import www.houdoudou.com.common.CommonMathFunction;
import www.houdoudou.com.common.ConnFactory;

public class ModifyPicUrlInTbProduct {

	public  static  void modify(){
		
		 PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
		   try{
			   String sql = "select Product_PicDir  from Tb_Product ";    
				stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
				rs= stm.executeQuery(sql);
			    rs.next();
			    String Product_PicDir= rs.getString("Product_PicDir");
			    
			    String[] nameparse=Product_PicDir.split("\\/");
		        
		         String IPnow=getHostIP()+":8080";
		         String Product_PicDir_Now;
		        if(!nameparse[2].equals(IPnow)) {
		        	//更新本行
		        	nameparse[2]=IPnow;
		        	Product_PicDir_Now ="";
		        	  for(int i=0;i<nameparse.length-1;i++){
		        		  Product_PicDir_Now+=nameparse[i]+"/";
		        	  }
		        	  Product_PicDir_Now+=nameparse[nameparse.length-1];
		        	 rs.updateString("Product_PicDir", Product_PicDir_Now);
		        	 rs.updateRow();
		        	 // 跟新其余行
		        	 while(rs.next()){
		        		 Product_PicDir= rs.getString("Product_PicDir");
		        		 nameparse=Product_PicDir.split("\\/");
		        		 nameparse[2]=IPnow;
		        		 Product_PicDir_Now ="";
		        		 for(int i=0;i<nameparse.length-1;i++){
			        		  Product_PicDir_Now+=nameparse[i]+"/";
			        	  }
			        	  Product_PicDir_Now+=nameparse[nameparse.length-1];
		        		 rs.updateString("Product_PicDir", Product_PicDir_Now);
			        	 rs.updateRow();
		        	 }
		        	 System.out.println("-- update Pic_Dir Success---"); 
		        }else{
		        	 System.out.println("-- update Pic_Dir NoNeed---"); 
		        }
		        
			   
		   }catch(Exception e){
			   e.printStackTrace();
			   
			   System.out.println("-- update Pic_Dir Failed---"); 
		   }finally{
			   ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
		   }
		
	}
	
	
	public static String getHostIP(){
		String IP="";
		
		try {
			InetAddress inet = InetAddress.getLocalHost();
			IP=inet.getHostAddress();
			System.out.println("------IP("+IP+")---------");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return IP;
	}
	
	public  static  void testInventory(){
		
		 PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
		   try{
			   String sql = "select *  from Tb_Box_Inventory where Date=201507231200";    
				stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
				rs= stm.executeQuery(sql);
			    rs.next();
			    System.out.println("------------------------");
			    System.out.println(rs.getString("Date"));
			    System.out.println(rs.getString("TimFlag"));
			    System.out.println(rs.getInt("Inventory"));
			    System.out.println(rs.getInt("Machine_ID"));
			    System.out.println("------------------------");
			   
		   }catch(Exception e){
			   e.printStackTrace();
			   
			   System.out.println("-- update Pic_Dir Failed---"); 
		   }finally{
			   ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
		   }
	}
}
